package com.wbs.rest.resource;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ForwardedHeader {
    public static String NAME = "Forwarded";
    private static final ForwardedHeader NO_HEADER = new ForwardedHeader(
            Collections.emptyMap());
    private final Map<String, String> elements;

    private ForwardedHeader(Map<String, String> elements) {
        this.elements = elements;
    }

    public static ForwardedHeader of(String source) {
        if (!StringUtils.hasText(source)) {
            return NO_HEADER;
        } else {
            Map<String, String> elements = new HashMap();
            String[] var2 = source.split(";");
            int var3 = var2.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String part = var2[var4];
                String[] keyValue = part.split("=");
                if (keyValue.length == 2) {
                    elements.put(keyValue[0].trim(), keyValue[1].trim());
                }
            }

            Assert.notNull(elements, "Forwarded elements must not be null!");
            Assert.isTrue(!elements.isEmpty(), "At least one forwarded element needs to be present!");
            return new ForwardedHeader(elements);
        }
    }

    public String getProto() {
        return (String)this.elements.get("proto");
    }

    public String getHost() {
        return (String)this.elements.get("host");
    }
}
